<?php
/**
 * @version 1.0 上传逻辑类
 * @internal
 * @link
 * @property
 * @see
 * @since
 * @uses
 *
 */
namespace app\user\logic;

use app\Api;

class Express extends Api
{

    private $key; //key值（快递100）

    private $customer; //customer

    private static $obj = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->key = config('kuaidi100.key');
        $this->customer = config('kuaidi100.customer');

    }

    /**
     * 类入口
     * @return Express|null
     */
    public static function getInstance()
    {
        if (is_null(self::$obj))
            self::$obj = new self();
        return self::$obj;
    }

    /**
     * 物流查询接口
     * @param $number string 快递单号
     * @param string $com string 公司简称
     * @return array|bool
     */
    public function express($number, $com = '')
    {
        if (!empty($com)) {
            $express = $this->expressInfo($com, $number);
            if (isset($express['status']) && $express['status'] == 200) {
                return ['com' => $express['com'], 'num' => $express['nu'], 'state' => $express['state'], 'data' => $express['data']];
            }
        } else {
            $company_list = $this->autoNumber($number);
            foreach ($company_list as $value) {
                $express = $this->expressInfo($value['comCode'], $number);
                if (isset($express['status']) && $express['status'] == 200) {
                    return ['com' => $express['com'], 'num' => $express['nu'], 'state' => $express['state'], 'data' => $express['data']];
                }
            }
        }
        return false;
    }

    /**
     * 快递100单号归属公司智能判断接口
     * @param $num string 快递公司单号
     *
     * [{
     * "comCode":"shunfeng",
     * "id":"",
     * "noCount":302236,
     * "noPre":"9069",
     * "startTime":""
     * },{
     * "comCode":"yuantong",
     * "id":"",
     * "noCount":24,
     * "noPre":"9069",
     * "startTime":""
     * }]
     */
    public function autoNumber($num)
    {
        $url = "http://www.kuaidi100.com/autonumber/auto?num={$num}&key={$this->key}";

        $info = json_decode($this->_curlGet($url), true);

        return $info;
    }

    /**
     * 快递信息
     * @param $com string 快递公司代码
     * @param $num string 快递公司单号
     * @return mixed
     * {
     * "message":"ok",
     * "state":"0",
     * "status":"200",
     * "condition":"F00",
     * "ischeck":"0",
     * "com":"yuantong",
     * "nu":"V030344422",
     * "data":[
     * {
     * "context":"上海分拨中心/装件入车扫描 ",
     * "time":"2012-08-28 16:33:19",
     * "ftime":"2012-08-28 16:33:19",
     * },
     * {
     * "context":"上海分拨中心/下车扫描 ",
     * "time":"2012-08-27 23:22:42",
     * "ftime":"2012-08-27 23:22:42",
     * }]
     * }
     */
    public function expressInfo($com, $num)
    {
        $url = 'http://poll.kuaidi100.com/poll/query.do';

        $post_data = array();
        $post_data["customer"] = $this->customer;
        $post_data["param"] = '{"com":"' . $com . '","num":"' . $num . '"}';
        $post_data["sign"] = strtoupper(md5($post_data["param"] . $this->key . $post_data["customer"]));

        $post_data = $this->handleString($post_data);

        return json_decode($this->_curlPost($url, $post_data), true);
    }

    /**
     * 对数据进行处理
     * @param $array
     * @return string
     */
    public function handleString($array)
    {
        $o = "";
        foreach ($array as $k => $v) {
            $o .= "$k=" . urlencode($v) . "&";        //默认UTF-8编码格式
        }
        $array = substr($o, 0, -1);
        return $array;
    }
}
